﻿using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping
{
    public partial class ProductInfo : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.GetLoginInfo() != null)
                {
                    LoginInfo loginInfo = Request.GetLoginInfo();
                    Session.SetLoginInfo(loginInfo);
                }
                if (Request.QueryString["ProductCode"] == null)
                {
                    Response.GoToAccessDeniedPage(Request);
                }
            }

            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int code = int.Parse(Request.QueryString["ProductCode"]);
                Product product = DbProduct.GetByCode(code);
                if (product == null)
                {
                    Response.GoToAccessDeniedPage(Request);
                }
                else
                {
                    lblProductName.Text = product.Name;
                    divContents.InnerHtml = product.Description;
                    lblPrice.Text = string.Format("{0:#,##0} VNĐ", product.Price);
                    lblMadeIn.Text = product.MadeIn;
                    DbStore.InitGridView(gvMyStores, code);
                    DbProductComment.InitGridView(gv_Comments, code);
                }
            }
        }

        protected void lbSelect_Click(object sender, EventArgs e)
        {
            LinkButton lb = (sender as LinkButton);
            int StoreID = int.Parse(lb.CommandArgument);
            iFrame.Attributes["src"] = string.Format("StoreInMap.aspx?SelectedStore={0}", StoreID);
            PopupUpdate.Show();
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Length > 0)
            {
                if (txtComment.Text.Length >= 50)
                {
                    int code = int.Parse(Request.QueryString["ProductCode"]);
                    if (DbProductComment.Comment(txtComment.Text, "Hiển", code))
                    {
                        DbProductComment.InitGridView(gv_Comments, code);
                    }
                }
                else
                {
                    MyPopup.InitPopup("Lỗi", "Nhận xét phải ít nhất 50 kí tự");
                    MyPopup.Show();
                }
            }
            else
            {
                MyPopup.InitPopup("Lỗi", "Bạn chưa nhập tên người nhận xét");
                MyPopup .Show();
            }
        }
    }
}